// 购物车状态
import { insertCart, findCartList, deleteCart, updateCart, checkAllCart } from '@/api/cart'

export default {
  namespaced: true,
  state: () => ({
    cartList: []
  }),
  getters: {
    effectiveList (state) {
      return state.cartList.filter(item => item.isEffective)
    },
    // 计算有效的购物车列表数据的总量
    effectiveCount (state, getters) {
      return getters.effectiveList.reduce((a, c) => {
        return a + c.count
      }, 0)
    },
    effectiveTotal (state, getters) {
      return getters.effectiveList.reduce((a, c) => {
        return a + c.count * c.nowPrice
      }, 0)
    },
    // 是否全选
    isAllSelected (state, getters) {
      return getters.effectiveList.every(item => item.selected === true)
    },
    selectedCount (state, getters) {
      return getters.effectiveList.filter(item => item.selected).reduce((a, c) => {
        return a + c.count
      }, 0)
    },
    allPrice (state, getters) {
      return getters.effectiveList.filter(item => item.selected).reduce((a, c) => {
        return a + c.count * c.nowPrice
      }, 0)
    }
  },
  mutations: {
    // 添加购物车
    addCart (state, list) {
      state.cartList = list
    },
    // 更新购物车
    update (state, list) {
      state.cartList = list
    },
    // 单选
    singleCheck (state, { skuId, selected }) {
      const item = state.cartList.find(item => item.skuId === skuId)
      item.selected = selected
    }
  },
  actions: {
    // 添加
    asyncAddCart (ctx, { skuId, count }) {
      return new Promise((resolve) => {
        // 1.调用接口
        insertCart({ skuId, count }).then((res) => {
          // 拉取最新的购物车列表
          findCartList().then(res => {
            ctx.commit('addCart', res.result)
            resolve()
          })
        })
      })
    },
    // 删除
    asyncDelCart (ctx, ids) {
      return new Promise((resolve) => {
        deleteCart(ids).then(() => {
          // 拉取最新的购物车列表
          findCartList().then(res => {
            ctx.commit('addCart', res.result)
            resolve()
          })
        })
      })
    },
    // 更新
    updateCartList (ctx) {
      findCartList().then(res => {
        ctx.commit('update', res.result)
      })
    },
    // 单选
    asyncSingleCheck (ctx, good) {
      return new Promise((resolve) => {
        updateCart(good).then(res => {
          findCartList().then(res => {
            ctx.commit('update', res.result)
          })
          resolve()
        })
      })
    },
    // 全选
    asyncAllCheck (ctx, selected) {
      return new Promise((resolve) => {
        const params = {
          selected,
          id: ctx.getters.effectiveList.map(item => item.skuId)
        }
        checkAllCart(params).then(res => {
          findCartList().then(res => {
            ctx.commit('update', res.result)
          })
          resolve()
        })
      })
    },
    // 修改数量
    asyncCount (ctx, good) {
      return new Promise((resolve) => {
        updateCart(good).then(res => {
          findCartList().then(res => {
            ctx.commit('update', res.result)
          })
          resolve()
        })
      })
    }
  }
}
